#include<stdio.h>
struct map
{
	char a[100][100];
	int book[100][100];
	int dp[100][100];
};
struct map b[50];
int k,n,m;
int i,j,u,is,ie,js,je,us,ue;
int next[7][3]={ {1,0,0},{0,0,1},{0,1,0},{0,-1,0},{0,0,-1},{-1,0,0} };
void dfs(int startu,int starti,int startj,int time)
{
	int z;
	int up,ip,jp;
	if(b[startu].a[starti][startj]=='E'&&b[startu].dp[starti][startj]>time)
	{
		b[startu].dp[starti][startj]=time;
		return;
	}
	if(b[startu].dp[starti][startj]>time)
		b[startu].dp[starti][startj]=time;
	else
		return;
	for(z=0;z<6;z++)
	{
		up=startu+next[z][0];
		ip=starti+next[z][1];
		jp=startj+next[z][2];
		if(up>k||up<1)
			continue;
		else if(ip<0||ip>=n)
			continue;
		else if(jp<0||jp>=m)
			continue;
		if(b[up].book[ip][jp]==0&&b[up].a[ip][jp]!='#')
		{
			b[up].book[ip][jp]=1;
			dfs(up,ip,jp,time+1);
			b[up].book[ip][jp]=0;
		}
	}
	return;
}
int main()
{
	int flags,flage;
	while(1)
	{
		scanf("%d%d%d",&k,&n,&m);
		if(!k&!n&!m)
			break;
		flags=1;
		flage=1;
		for(u=1;u<=k;u++)
		{
			for(i=0;i<n;i++)
			{
				scanf("%s",b[u].a[i]);
				for(j=0;j<m&&flags;j++)
					if(b[u].a[i][j]=='S')
					{
						flags=0;
						is=i;js=j;us=u;
					}

				for(j=0;j<m&&flage;j++)
					if(b[u].a[i][j]=='E')
					{
						flage=0;
						ie=i;je=j;ue=u;
					}
			for(j=0;j<m;j++)
				b[u].dp[i][j]=1e7;
			}
			getchar();
		}
		dfs(us,is,js,0);
		
		if(b[ue].dp[ie][je]==1e7)
			printf("-1\n");
		else
			printf("%d\n",b[ue].dp[ie][je]);
	}
	return 0;
}
